package net.maku.system.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.framework.common.entity.WxUserInfoEntity;
import net.maku.framework.common.utils.Result;
import net.maku.framework.security.user.SecurityUser;
import net.maku.framework.security.user.UserDetail;
import net.maku.framework.security.utils.TokenUtils;
import net.maku.member.vo.MemberUserTokenVO;
import net.maku.system.service.SysAuthService;
import net.maku.system.service.SysCaptchaService;
import net.maku.system.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 认证管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("sys/auth")
@Tag(name = "认证管理")
@AllArgsConstructor
public class SysAuthController {
    private final SysCaptchaService sysCaptchaService;
    private final SysAuthService sysAuthService;

    @PostMapping("login")
    @Operation(summary = "账号密码登录")
    public Result<MemberUserTokenVO> login(@RequestBody SysAccountLoginVO login) {
        MemberUserTokenVO token = sysAuthService.loginByAccount(login);
        return Result.ok(token);
    }

    @PostMapping("loginByUnionId")
    @Operation(summary = "微信的unionId登录")
    public Result<MemberUserTokenVO> loginByUnionId(@RequestBody @Valid UnionLoginVO login){
        MemberUserTokenVO token = sysAuthService.loginByUnionId(login);
        return Result.ok(token);
    }

    @GetMapping("getMobileByCode")
    @Operation(summary = "获取微信用户的手机号")
    public Result getMobileByCode(String code){
        if(StringUtils.isBlank(code)){
            return Result.error("code不能为空");
        }
        LoginUserVo wxUserMobile = sysAuthService.getWxUserMobile(code);
        return Result.ok(wxUserMobile);
    }

    @GetMapping("getUserInfoByMobile")
    @Operation(summary = "获取微信用户的手机号")
    public Result getUserInfoByMobile(String mobile){
        if(StringUtils.isBlank(mobile)){
            return Result.error("手机号不能为空");
        }
        LoginUserVo wxUserMobile = sysAuthService.getUserInfoByMobile(mobile);
        return Result.ok(wxUserMobile);
    }

    @GetMapping("getWxUserInfo")
    @Operation(summary = "获取用户在微信的信息")
    public Result<WxUserInfoVO> getWxUserInfo(String code){
        if(StringUtils.isBlank(code)){
            return Result.error("code不能为空");
        }
        WxUserInfoVO userInfo = sysAuthService.getWxUserInfo(code);
        return Result.ok(userInfo);
    }

    @PostMapping("bindWx")
    @Operation(summary = "账号绑定微信")
    public Result bindWx(@RequestBody @Valid BindAccountVo bindAccountVo){
        sysAuthService.bindAccount(bindAccountVo);
        return Result.ok();
    }

    @PostMapping("register")
    @Operation(summary = "用户注册")
    public Result<WxUserInfoEntity> register(@RequestBody @Valid RegisterVO registerVO){
        WxUserInfoEntity register = sysAuthService.register(registerVO);
        return Result.ok(register);
    }

    @GetMapping("getLoginInfo")
    @Operation(summary = "账号密码登录")
    public Result getUserInfo(){
        UserDetail user = SecurityUser.getUser();
        LoginUserVo loginUserVo = BeanUtil.copyProperties(user, LoginUserVo.class);
        loginUserVo.setMemberId(user.getId());
        return Result.ok(loginUserVo);
    }

    @PostMapping("logout")
    @Operation(summary = "退出")
    public Result<String> logout(HttpServletRequest request) {
        sysAuthService.logout(TokenUtils.getAccessToken(request));

        return Result.ok();
    }
}
